import React from 'react';
import { Spin } from 'antd';
import { Container } from '@/components/Container';
function removeHTMLTagsExceptB(input) {
  return input.replace(/<(?!\/?b\s*\/?>)[^>]*>/g, '');
}
export function NewsContent({ newsData, loading }) {


  const NewsArticle = ({ title, summary, main_content, docdate, url }) => {
    if (typeof main_content !== 'string' || main_content.trim() === '') {
      return null;
    }
    main_content = removeHTMLTagsExceptB(main_content)
    const contentParagraphs = main_content.split('\n').filter(paragraph => paragraph.trim() !== '');
    const newsTitle = contentParagraphs[0];
    const newsTags = contentParagraphs[1];
    const contentWithoutTitle = contentParagraphs.slice(2, -2).map((paragraph, index) => (
      <p className='mt-5' key={index} dangerouslySetInnerHTML={{ __html: paragraph }}>{ }</p>
    ));
    const newsFrom = contentParagraphs[contentParagraphs.length - 1]
    return (
      <Container className='bg-white max-w-5xl'>
        <div className='p-20'>
          <h2 className='text-3xl font-semibold mb-2 text-center'>{newsTitle}</h2>
          <div className='text-sm mt-5 bg-gray-100 p-2 px-10'> {newsTags}</div>
          <div className="text-base mt-10 leading-8 tracking-normal" >{contentWithoutTitle}</div>
          <div className="text-right text-sm mt-5 bg-gray-100 p-2 px-10"> {newsFrom} </div>
          <p className="text-right mt-5 px-10">
            原文链接：<a href={url} target="_blank" rel="noopener noreferrer">查看原文</a>
          </p>
        </div>
      </Container>
    )
  };
  return (
    <Spin spinning={loading}>
      {loading ? <p className='text-center font-bold m-10 text-lg'>Loading...</p> : <NewsArticle {...newsData} />}
    </Spin>
  );
}

